$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/student/list',
        datatype: "json",
        colModel: [			
			{ label: '', name: 'studentno', index: 'StudentNo', hidden:true,width: 50, key: true },
			{ label: '', name: 'counselno', index: 'counselNo', hidden:true,width: 80 }, 			
			{ label: '姓名', name: 'name', index: 'Name', width: 80 }, 			
			{ label: '性别', name: 'gender', index: 'Gender', width: 80 }, 			
			{ label: '出生日期', name: 'birthday', index: 'Birthday', width: 80 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'}}, 	
			{ label: '手机号码', name: 'phone', index: 'phone',width: 80 }, 
			{ label: '', name: 'studentid', index: 'StudentId',hidden:true, width: 80 }, 			
			{ label: '', name: 'schoolno', index: 'SchoolNo', hidden:true,width: 80 }, 			
			{ label: '', name: 'grade', index: 'Grade', hidden:true,width: 80 }, 			
			{ label: '', name: 'subject', index: 'Subject',hidden:true, width: 80 }, 			
			{ label: '', name: 'remark', index: 'Remark', hidden:true,width: 80 }, 			
			{ label: '', name: 'studyinfo', index: 'StudyInfo',hidden:true, width: 80 }, 			
			{ label: '', name: 'webchat', index: 'webchat',hidden:true, width: 80 }, 			
			{ label: '', name: 'address', index: 'address', width: 80, hidden:true}, 			
			{ label: '是否VIP', name: 'vipflag', index: 'vipflag', width: 80}, 			
			{ label: '', name: 'reportflag', index: 'reportflag', width: 80 ,hidden:true}, 			
			{ label: '', name: 'reportno', index: 'ReportNo', width: 80, hidden:true}, 			
			{ label: '', name: 'standbyflag4', index: 'StandByFlag4', width: 80, hidden:true}, 			
			{ label: '', name: 'standbyflag2', index: 'StandByFlag2', width: 80, hidden:true}, 			
			{ label: '', name: 'standbyflag3', index: 'StandByFlag3', width: 80, hidden:true }, 			
			{ label: '', name: 'makeoperator', index: 'MakeOperator', width: 80 , hidden:true}, 			
			{ label: '', name: 'makedate', index: 'MakeDate', width: 80 , hidden:true}, 			
			{ label: '', name: 'maketime', index: 'MakeTime', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifyoperator', index: 'ModifyOperator', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifydate', index: 'ModifyDate', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifytime', index: 'ModifyTime', width: 80 , hidden:true}			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			commonQuery: null,
			GradeclassNo:null,
			classNo:null,
			SchoolNo:null,
			VIPFlag:null,
			state:null,
			startDate:null,
			endDate:null,
			studydean:null,
			name:null,
			phone:null
		},
		showList: true,
		title: null,
		student: {}
	},
	methods: {
		reset:function (){//重置查询条件
			vm.q.commonQuery ="";
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.student = {};
			this.initSchool();
	        this.initGrade();
	        this.initSubject();
		},
		update: function (event) {
			var studentno = getSelectedRow();
			this.initSchool();
	        this.initGrade();
	        this.initSubject();
			if(studentno == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(studentno)
            
		},
		saveOrUpdate: function (event) {
			var url = vm.student.studentno == null ? "generator/student/save" : "generator/student/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.student),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var studentnos = getSelectedRows();
			if(studentnos == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/student/delete",
                    contentType: "application/json",
				    data: JSON.stringify(studentnos),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(studentno){
			$.get(baseURL + "generator/student/info/"+studentno, function(r){
                vm.student = r.student;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				 postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		},
		exportExcl:function(){
			var wb = XLSX.utils.table_to_book(document.getElementById('jqGrid'));
			wb.Sheets.Sheet1.A1={t:"s",v:"tt"};
			wb.Sheets.Sheet1.B1={t:"s",v:"tt"};
			wb.Sheets.Sheet1.C1={t:"s",v:"tt"};
			T.exportExcel(wb,"test.xlsx","")
		},
		dropout:function(){

			var studentnos = getSelectedRows();
			if(studentnos == null){
				return ;
			}
			
			confirm('确定选中的学生要退学？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/student/dropout",
                    contentType: "application/json",
				    data: JSON.stringify(studentnos),
				    success: function(r){
						if(r.code == 0){
							alert('退学成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		
			
		},
		initSchool:function(){
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/school/findAllSchool",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $school = $("#schoolno");
						$school.empty();
						$school.append("<option selected=''>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i ==  0 ){
								$school.append("<option selected='selected' value='"+page[i].schoolno+"'>"+page[i].schoolname+"</option>")
							}else{
								$school.append("<option value='"+page[i].schoolno+"'>"+page[i].schoolname+"</option>")
							}
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		initGrade:function(){
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/grade/findAllGrade",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $grade = $("#grade");
						$grade.empty();
						$grade.append("<option selected='selected'>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i == 0){
								$grade.append("<option selected='selected' value='"+page[i].code+"'>"+page[i].name+"</option>");
							}else{
								$grade.append("<option value='"+page[i].code+"'>"+page[i].name+"</option>");
							}
							
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		initSubject:function(){//初始化学科信息
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/discipline/findAllDiscipline",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $subject = $("#subject");
						$subject.empty();
						$subject.append("<option selected='selected'>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i == 0){
								$subject.append("<option selected='selected' value='"+page[i].disciplineno+"'>"+page[i].disciplinename+"</option>");
							}else{
								$subject.append("<option value='"+page[i].disciplineno+"'>"+page[i].disciplinename+"</option>");
							}
							
						}
					}else{
						alert(r.msg);
					}
				}
			});
		}
	},
	mounted:function(){//初始化后的方法
		
    }
});
